import { IScreenShotOptions, ScreenShot } from '../misc/screen-shot';
import { Engine } from '@babylonjs/core/Engines/engine';
import { Camera } from '@babylonjs/core/Cameras/camera';
import { decode, encode } from 'js-base64';


/**
 * 工具类工程的截图功能（接口截图及开发缩略图截取）
 */
class CaptureScreen {

    /**
     * 是否输出调试信息
     */
    public static debug = false;

    /**
     * 设置截图所需组件
     * @param engine 引擎
     * @param camera 截图相机（一般为活动相机）
     * @param isProduction 是否生产环境
     * @param appName 工程名称
     */
    public static register(engine: Engine, camera: Camera, isProduction: boolean, appName: string): void {
        if (this.debug) console.log('正在注册 window.toolCaptureScreenShot 截图方法,当前是否为生产环境:', isProduction);
        (window as any).toolCaptureScreenShot = (optionBase64: any) => {
            let option = null;
            if (optionBase64) {
                try {
                    const tJson = decode(optionBase64);
                    option = JSON.parse(tJson);
                } catch (err) {
                    console.error('Parameter parsing failed:', err);
                }
            }
            if (this.debug) console.log('正在调用 window.toolCaptureScreenShot 截图方法,参数设置:', option);
            CaptureScreen.capture(engine, camera, isProduction, appName);
        };

    }

    /**
     * 截取平面渲染区域
     * @param engine 引擎
     * @param camera 截图相机
     * @param isProduction 是否生产环境
     * @param appName 工程名称
     */
    public static capture(engine: Engine, camera: Camera, isProduction: boolean, appName: string): void {
        if (this.debug) console.log('正在自动设置截图参数.');
        const options: IScreenShotOptions = {};
        if (isProduction) {
            options.useRenderTarget = false;
            options.download = false;
            options.imagesInformation = [{ width: 1024, height: 768 }];
        } else {
            options.useRenderTarget = true;
            options.imagesInformation = [
                {
                    name: 'thumb',
                    width: 160,
                    height: 90,
                },
                {
                    name: 'thumb1',
                    width: 400,
                    height: 225,
                },
                {
                    name: 'thumb2',
                    width: 800,
                    height: 600,
                }
            ]
        }
        if (this.debug) console.log('开始截图.');
        ScreenShot.save(engine, camera, options).then((datas: string[]) => {
            if (!isProduction) return;
            if (!datas.length) return;
            if (this.debug) console.log('截图成功,如果 window.tooJS.toolTransScreenShot 方法已经注册,则会转换数据并调用该方法.');
            const tooJS = (window as any).tooJS;
            let toolTransScreenShot: { (data: string): void } | null = null;
            if (tooJS) {
                toolTransScreenShot = tooJS.toolTransScreenShot;
            }
            if (!toolTransScreenShot) {
                if (this.debug) console.log('window.tooJS.toolTransScreenShot 方法不存在,操作终止.');
                return;
            }

            try {
                const data = datas[0];
                const tmp = {
                    img: data.replace('data:image/png;base64,', ''),
                    url: window.location.href,
                    name: appName,
                };
                const str = JSON.stringify(tmp);
                const b64 = encode(str);
                if (this.debug) console.log('数据转换完成,正在调用接口方法 window.tooJS.toolTransScreenShot.');
                toolTransScreenShot(b64);
            } catch (err) {
                console.error(err);
            }

        });
    }

}


export { CaptureScreen };